<template>
    <div class="admin-page">
        <div class="page-header">
            <div style="display: flex;justify-content: space-between">
                <div style="display: flex; flex-wrap: wrap; align-items: center; gap: 15px; margin-bottom: 15px;">
                    <div style="display: flex; align-items: center;" class="search-item">
                        <span class="search-label">搜索:</span>
                        <el-input
                            v-model="searchForm.search"
                            size="small"
                            style="width: 130px;"
                            placeholder="请输入"
                        />
                    </div>
<!--                    <div style="display: flex; align-items: center;" class="search-item">
                        <span class="search-label">修改类型:</span>
                        <el-select v-model="searchForm.type" size="small" style="width: 120px;" placeholder="全部">
                            <el-option label="前台用户修改" value="1"></el-option>
                            <el-option label="后台修改" value="2"></el-option>
                        </el-select>
                    </div>-->
                    <div style="display: flex; align-items: center;" class="search-item">
                        <span class="search-label">日期:</span>
                        <el-date-picker
                            size="small"
                            v-model="searchForm.start_time"
                            type="date"
                            style="width: 150px;"
                            placeholder="开始日期">
                        </el-date-picker>
                        <span style="margin: 0 5px;">至</span>
                        <el-date-picker
                            size="small"
                            style="width: 150px;"
                            v-model="searchForm.end_time"
                            type="date"
                            placeholder="结束日期">
                        </el-date-picker>
                    </div>
                </div>
                <div style="display: flex; align-items: center; gap: 15px; margin-bottom: 15px;">
                    <el-button type="primary" size="small" @click="handleSearch">搜索</el-button>
                    <el-button type="success" size="small" style="margin-left: 10px;" @click="handleReset">重置</el-button>
                </div>
            </div>

            <el-table :data="tableData" height="560" border style="width: 100%;overflow-y: auto"
                      :header-cell-style="{background:'#FAFAFA',color:'#666', fontSize:'13px'}" size="small">
                <el-table-column prop="id" label="id" width="100" align="center" />
                <el-table-column prop="admin_id" label="admin_id" width="100" align="center" />
                <el-table-column prop="title" label="标题" width="160" align="center" />
                <el-table-column prop="url" label="操作地址"  align="center" />
                <el-table-column prop="admin_name" label="操作人" width="100"  align="center" />
                <el-table-column prop="create_time" label="操作时间" width="160" align="center" />

<!--                <el-table-column prop="module_name" label="操作模块" width="160" align="center" />
                <el-table-column prop="controller_name" label="控制器名" width="160" align="center" />
                <el-table-column prop="action_name" label="方法名" width="160" align="center" />-->
                <el-table-column label="详情"   align="center" width="120">
                    <template slot-scope="scope">
                        <el-button type="text" size="mini" @click="toDetail(scope.row)" icon="el-icon-edit">详情
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-dialog
                title="提示"
                :visible.sync="centerDialogVisible"
                width="50%"
                center>
                <el-form :model="detailList" ref="detailList" label-width="100px" size="small">
                    <el-form-item label="id">
                        <span>{{ detailList.id }}</span>
                    </el-form-item>
                    <el-form-item label="标题">
                        <span>{{ detailList.title }}</span>
                    </el-form-item>
                    <el-form-item label="操作地址">
                        <span>{{ detailList.url }}</span>
                    </el-form-item>
                    <el-form-item label="操作人">
                        <span>{{ detailList.admin_name }}</span>
                    </el-form-item>
                    <el-form-item label="操作时间">
                        <span>{{ detailList.create_time }}</span>
                    </el-form-item>
                    <el-form-item label="params">
                        <div style="white-space: pre-wrap; word-break: break-all;">{{ detailList.parms }}</div>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                <el-button @click="centerDialogVisible = false" size="small">取 消</el-button>
                <el-button type="primary" @click="centerDialogVisible = false" size="small">确 定</el-button>
              </span>
            </el-dialog>
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-size="pageSize"
                layout="total, prev, pager, next"
                :total="total"
            />
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            searchForm: {
                start_time: '',
                end_time: '',
                search: '',
                type:""
            },
            centerDialogVisible: false,
            tableData: [],
            currentPage: 1,
            pageSize: 15,
            total: 0,
            searchParams:{},
            detailList:{}
        }
    },
    created() {
        this.getKuaidi()
    },
    methods: {
        toDetail(e) {
            const item = this.tableData.find(item => item.id === e.id)
            this.detailList = item
            this.centerDialogVisible = true
        },
        getKuaidi(params={}) {
            const mergedParams = {
                ...params,  // 先展开传入的参数
                page: params.page || this.currentPage,  // 优先使用传入的页码
            };
            this.$http.get('/admin/OperLog/lists',{params:mergedParams}).then(result => {
                // 假设result.data就是你获取到的原始对象数据
                this.tableData  = result.data.data;
                this.total = result.data.total || 0; // Add total count
            });
        },
        handleSearch() {
            const formatDate = (date) => {
                if (!date) return '';
                const d = new Date(date);
                const year = d.getFullYear();
                const month = String(d.getMonth() + 1).padStart(2, '0');
                const day = String(d.getDate()).padStart(2, '0');
                const hours = String(d.getHours()).padStart(2, '0');
                const minutes = String(d.getMinutes()).padStart(2, '0');
                const seconds = String(d.getSeconds()).padStart(2, '0');
                const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
                const timestamp = new Date(formattedDate).getTime();
                return Math.floor(timestamp / 1000); // 转换为秒级时间戳
            };

            this.searchParams = {
                ...this.searchForm,
                start_time: formatDate(this.searchForm.start_time),
                end_time: formatDate(this.searchForm.end_time)
            };
            // Remove empty string properties from searchParams before sending
            Object.keys(this.searchParams).forEach(key => {
                if (this.searchParams[key] === '') {
                    delete this.searchParams[key];
                }
            });
            this.currentPage = 1;
            this.getKuaidi(this.searchParams);
        },
        handleReset() {
            this.searchForm = {

            }
            this.getKuaidi()
        },
        handleSizeChange(val) {
            this.pageSize = val;
            this.currentPage = 1;
            this.getKuaidi({...this.searchParams, page: this.currentPage, limit: val});
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.getKuaidi({...this.searchParams, page: val, limit: this.pageSize});
        }
    }
}
</script>
<style scoped lang="less">
.admin-page {
    padding: 20px;

    .search-label {
        margin-right: 10px;
        color: #606266;
    }

    .el-table {
        margin-top: 20px;
    }

    .el-pagination {
        margin-top: 20px;
        text-align: right;
    }
}
</style>
